'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class IcqNotificationService
    Inherits ContextService
    Implements Interfaces.INotificationService

    Public Sub New(ByVal context As Interfaces.IContext)
        MyBase.New(context)

        Dim connector As IcqConnector = TryCast(context.GetService(Of Interfaces.IConnector)(), IcqConnector)

        If connector Is Nothing Then Throw New InvalidCastException("Context Connector Service must be of Type IcqConnector")

        connector.RegisterSnacHandler(Of DataTypes.Snac0414)(&H4, &H14, New Action(Of DataTypes.Snac0414)(AddressOf AnalyseSnac0414))
    End Sub

    Private Sub AnalyseSnac0414(ByVal snac As DataTypes.Snac0414)
        Try
            Dim contact As Interfaces.IContact
            Dim type As Interfaces.NotificationType

            contact = Context.GetService(Of Interfaces.IStorageService).GetContactByIdentifier(snac.ScreenName)

            Select Case snac.NotificationType
                Case DataTypes.NotificationType.TextCleared
                    type = Interfaces.NotificationType.TypingCanceled
                Case DataTypes.NotificationType.TextTyped
                    type = Interfaces.NotificationType.TypingFinished
                Case DataTypes.NotificationType.TypingBegun
                    type = Interfaces.NotificationType.TypingStarted
            End Select

            RaiseEvent TypingNotificationEventArgs(Me, New Interfaces.TypingNotificationEventArgs(contact, type))
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Public Sub SendNotification(ByVal receiver As Interfaces.IContact, ByVal type As Interfaces.NotificationType) Implements Interfaces.INotificationService.SendNotification
        Dim dataOut As DataTypes.Snac0414

        dataOut = New DataTypes.Snac0414

        dataOut.Channel = DataTypes.MessageChannel.Channel1PlainText

        Select Case type
            Case Interfaces.NotificationType.TypingCanceled
                dataOut.NotificationType = DataTypes.NotificationType.TextCleared
            Case Interfaces.NotificationType.TypingFinished
                dataOut.NotificationType = DataTypes.NotificationType.TextTyped
            Case Interfaces.NotificationType.TypingStarted
                dataOut.NotificationType = DataTypes.NotificationType.TypingBegun
        End Select

        dataOut.ScreenName = receiver.Identifier

        Dim transfer As IIcqDataTranferService = DirectCast(Context.GetService(Of Interfaces.IConnector)(), IIcqDataTranferService)
        transfer.Send(dataOut)
    End Sub

    Public Event TypingNotificationEventArgs(ByVal sender As Object, ByVal e As Interfaces.TypingNotificationEventArgs) Implements Interfaces.INotificationService.TypingNotificationEventArgs
End Class
